//
//  ManageRootController.m
//  collection
//
//  Created by zhangjing on 12/21/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "ManageRootController.h"
#import "SettingController.h"
#import "ModifyPasswordController.h"
#import "SearchUserController.h"
#import "RecommendUserController.h"
#import "AboutViewController.h"
#import "FeedbackController.h"
#import "AppDelegate.h"

@interface ManageRootController()

@property (nonatomic, strong)   IBOutlet    UITableView*    tableView;
@property (nonatomic, strong)   NSArray*                    tableArray;

- (IBAction)onClickLogout:(id)sender;

@end

@implementation ManageRootController

@synthesize tableView   = _tableView;
@synthesize tableArray  = _tableArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
     //   self.title = @"管理";
        
        // Load table items
        NSArray* section1 = [NSArray arrayWithObjects:@"设置", @"修改密码", nil];
        NSArray* section2 = [NSArray arrayWithObjects:@"搜索用户", @"推荐用户", nil];
        NSArray* section3 = [NSArray arrayWithObjects:@"关于", @"意见反馈", nil];
        self.tableArray = [NSArray arrayWithObjects:section1, section2, section3, nil];
    }
    return self;
}

- (void)dealloc {
    
    self.tableView  = nil;
    self.tableArray = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"all_bg"]];
    
    if (self.tableView != nil) {
        [self.tableView reloadData];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.tableView  = nil;
    self.tableArray = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.tableArray != nil) {
        return [self.tableArray count];
    }
    else {
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.tableArray != nil && [self.tableArray count] > section) {
        NSArray* rowArray = [self.tableArray objectAtIndex:section];
        return [rowArray count];
    }
    else {
        return 0;
    }
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        return @"   添加关注";
    }
    else {
        return nil;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* manageCellID = @"ManageCell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:manageCellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:manageCellID] autorelease];
        cell.indentationLevel = 1;
        cell.indentationWidth = 15.0f;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.text = [[self.tableArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - Impelementation of UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UIViewController* settingController = [[SettingController alloc] initWithNibName:@"SettingController" bundle:nil];
            [appDelegate.rootNavigationController pushViewController:settingController animated:YES];
            [settingController release];
        }
        else if (indexPath.row == 1) {
            UIViewController* modifyPasswordController = [[ModifyPasswordController alloc] initWithNibName:@"ModifyPasswordController" bundle:nil];
            [appDelegate.rootNavigationController pushViewController:modifyPasswordController animated:YES];
            [modifyPasswordController release];
        }
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            UIViewController* searchUserController = [[SearchUserController alloc] initWithNibName:@"SearchUserController" bundle:nil];
            [appDelegate.rootNavigationController pushViewController:searchUserController animated:YES];
            [searchUserController release];
        }
        else if (indexPath.row == 1) {
            UIViewController* recommendUserController = [[RecommendUserController alloc] initWithNibName:@"RecommendUserController" bundle:nil];
            [appDelegate.rootNavigationController pushViewController:recommendUserController animated:YES];
            [recommendUserController release];
        }
    }
    
    else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            UIViewController* aboutController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
            [appDelegate.rootNavigationController pushViewController:aboutController animated:YES];
            [aboutController release];
        }
        else if (indexPath.row == 1) {
            UIViewController* feedbackController = [[FeedbackController alloc] initWithNibName:@"FeedbackController" bundle:nil];
            [appDelegate.rootNavigationController pushViewController:feedbackController animated:YES];
            [feedbackController release];
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Private methods

- (IBAction)onClickLogout:(id)sender {
    
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    //    [appDelegate.rootNavigationController popViewControllerAnimated:NO];
    [appDelegate.rootNavigationController popViewControllerAnimated:YES];
}

@end
